$(function() {
	//设置警告信息状态的接口
	var setWarningMessStatus = '';
	//获取警告信息的接口
	var getWarningMessUrl = "/guobo/trailLog/searchNoProcTrail";
	//确定已读警告的接口
	var getWarningMessUrl = "/guobo/trailLog/procTrail";
	
	console.log(getWarningMessUrl);
	
	var tips_sound = new esayTips("sound/tip.mp3");

	var warningArr = '';

	//ajax请求函数，参数依次为 ajax请求地址、传递的参数、请求方式、成功回调函数、错误回调函数
	function ajaxSend(_url, _type, _data, successFun, errorFun) {
		$.ajax({
			type: _type,
			url: _url,
			async: true,
			data: _data,
			success: function(data) {
				data = JSON.parse(data);
				if(data.code == 0) {
					if(successFun) successFun(data.data);
				} else {
    				alert('网络错误');
				}
			},
			error: function(err) {
				if(errorFun) errorFun(err);
			}
		});
		//var data = JSON.parse('[{"msg":"办公桌1,rfid[100001]被非法带出[南馆]","time":1491031890250,"traillogid":"1f83a14ce89848cbac2bb8c8e163d557","url":"http://172.16.144.38"},{"msg":"办公桌1,rfid[100001]被非法带出[南馆]","time":1491031350423,"traillogid":"cc3da013aa58488e8b0c2b84137b0e52","url":"http://172.16.144.38"},{"msg":"办公桌1,rfid[100001]被非法带出[南馆]","time":1491031110990,"traillogid":"6c4eb7e6c8614c30aaf3d93f878fc667","url":"http://172.16.144.38"}]');
		//successFun(data);
	}
	//检查新的数据里面是否含有旧的数据里面没有的项
	function checkDiff(_old, _new) {
		var sound = false;
		var render = false;
		var hasNew = false;
		_old = _old ? _old : [];
		$.each(_new, function(key, val) {
			var checkTime = true;
			$.each(_old, function(k, v) {
				if(val.date == v.date) {
					checkTime = false;
				}
			});
			if(checkTime) {
				hasNew = true;
			}
		});

		if(hasNew ||_old.length != _new.length) {
			render = sound = true;
		}

		if(render) {
			warningArr = _new;
		}

		return {
			tips: sound,
			refresh: render,
			newData: _new
		}
	}

	function alertTip(condition) {

		if(condition.refresh) {
			alertShow(condition.newData);
		}

	}
	
	Date.prototype.Format = function (fmt) {
	    var o = {
	        "M+": this.getMonth() + 1, //月份 
	        "d+": this.getDate(), //日 
	        "h+": this.getHours(), //小时 
	        "m+": this.getMinutes(), //分 
	        "s+": this.getSeconds(), //秒 
	        "q+": Math.floor((this.getMonth() + 3) / 3), //季度 
	        "S": this.getMilliseconds() //毫秒 
	    };
	    if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
	    for (var k in o)
	    if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
	    return fmt;
	}

	function alertShow(data) {
		var box = $("#alert_dialog_bk");
		var close_bt = $("#alert_dialog_close");
		var html = '';
		if(!box.length) {
			$("body").append("<div id='alert_dialog_bk'></div>");
			box = $("#alert_dialog_bk")
		}
		
		if(!close_bt.length) {
			$("body").append("<div id='alert_dialog_close'>"+data.length+"</div>");
			close_bt = $("#alert_dialog_close");
			close_bt.on('click', function(){
				if(box.is(":visible")){
					box.fadeOut(400);
					close_bt.addClass('close_bt_animate');
				}else{
					box.fadeIn(400);
					close_bt.removeClass('close_bt_animate');
				}
			})
		}

		if(data[0]) {
			box.show();
			close_bt.show();
			tips_sound.play();
		} else {
			box.hide();
			close_bt.hide();
			tips_sound.stop();
		}
		$.each(data, function(key, val) {
			html += '<div class="alert_dialog"><div class="alert_dialog_head"><div class="text_pad10">警告！</div></div>' +
				'<div class="alert_dialog_body"><div class="alert_dialog_body_cont">' + val.msg + '</div><div class="alert_dialog_body_date">' + dateFormat(val.time) + '</div></div><div class="alert_dialog_footer">' +
				'<a data-id="'+val.traillogid+'" class="alert_dialog_bt bt_danger" target="_blank" href="'+val.url+'">查看</a>' +
				'<a data-id="'+val.traillogid+'" class="alert_dialog_bt bt_info" target="_blank">标为已查</a></div></div>'
		});
		close_bt.text(data.length);
		box.html(html);
		setWarningStatus();
	}
	
	function dateFormat(_time)
	{
		return new Date(_time).Format('yyyy-MM-dd hh:mm:ss');
	}
	
	function setWarningStatus(){
		$(".alert_dialog .alert_dialog_bt").on('click', function(){
			var warningMessId = $(this).attr('data-id');
			var dialog = $(this).parents(".alert_dialog");
			if(warningMessId){
				ajaxSend(checkWarningStatus, 'post', {id: warningMessId}, function(data){
					dialog.remove();
				},function(){
					console.log('网络错误');
				})
				
			}
		})
	}

	function listen() {
		ajaxSend(getWarningMessUrl, 'get', {sad:"asd"},
			function(data) {
				var action = checkDiff(warningArr, data);
				console.log(action);
				alertTip(action);
				setTimeout(function() {
					listen();
				}, 1000);
			},
			function(err) {
				console.log('网络错误');
				setTimeout(function() {
					listen();
				}, 1000);
			}
		);
	}

	listen();
})